package com.canbot.u05.activity.customvoice;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.activity.customvoice.adapter.CustomActionAdapter;
import com.canbot.u05.activity.customvoice.bean.CustomActionData;
import com.canbot.u05.activity.customvoice.tool.GridSpacingItemDecoration;
import com.canbot.u05.utils.ToastTools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liu.heng  on 2018/5/24.
 * 自定义对话选择动作
 */
public class CustomActionActivity extends BaseActivityNoTitle implements CustomActionAdapter.OnItemClickListener {

        private RecyclerView mRecyclerView;

        private ImageView headIamgeView;

        private ImageView handIamgeView;

        private ImageView legIamgeView;

        private CustomActionAdapter mCustomActionAdapter;

        private List<CustomActionData> mCustomActionData;

        private Button btn_skip;

        private int checkid = 1;

        private String action_ID = "";

        private int type = 0;

        private String[] mHead;

        private String[] mArm;

        private String[] mBoot;

        private String[] mHeadId;

        private String[] mArmId;

        private String[] mBootId;


        private Handler mHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                        mCustomActionData.clear();
                        mCustomActionAdapter.setChecked(-1);
                        switch (msg.what) {
                                case 1:
                                        getHeadData();
                                        break;
                                case 2:
                                        getHandData();
                                        break;
                                case 3:
                                        getlegData();
                                        break;
                                default:
                                        break;
                        }
                        for (int i = 0; i < mCustomActionData.size(); i++) {
                                if (!TextUtils.isEmpty(action_ID)) {
                                        if (action_ID.equals(mCustomActionData.get(i).getActionID())) {
                                                mCustomActionAdapter.setChecked(i);
                                                break;
                                        }
                                }
                        }
                        mCustomActionAdapter.notify(mCustomActionData);
                }
        };


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_custom_action);
                final Intent intent = getIntent();
                action_ID = intent.getStringExtra("action");
                type = intent.getIntExtra("type", 0);
                mHead = getResources().getStringArray(R.array.head_text);
                mArm = getResources().getStringArray(R.array.arm_text);
                mBoot = getResources().getStringArray(R.array.boot_text);
                mHeadId = getResources().getStringArray(R.array.head_id);
                mArmId = getResources().getStringArray(R.array.arm_id);
                mBootId = getResources().getStringArray(R.array.boot_id);
                initView();
        }

        private void initView() {
                headIamgeView = (ImageView) findViewById(R.id.head_image);
                handIamgeView = (ImageView) findViewById(R.id.hand_image);
                legIamgeView = (ImageView) findViewById(R.id.leg_image);
                btn_skip = (Button) findViewById(R.id.btn_skip);
                if (type == 1) {//判断动作是修改还是添加 1为修改
                        btn_skip.setVisibility(View.GONE);
                }
                mRecyclerView = (RecyclerView) findViewById(R.id.my_gridView);
                mRecyclerView.setLayoutManager(new GridLayoutManager(CustomActionActivity.this, 5));  //网格布局，5
                mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(5, 56, false));
                mCustomActionData = new ArrayList<>();

                mHandler.sendEmptyMessage(checkid);

                mCustomActionAdapter = new CustomActionAdapter(this, mCustomActionData, this);

                mRecyclerView.setAdapter(mCustomActionAdapter);

                //默认选择头部动作
                headIamgeView.setVisibility(View.VISIBLE);
        }


        @Override
        public void onItemClick(View view) {
                final int i = (int) view.getTag();
                mCustomActionAdapter.setChecked(i);
                mCustomActionAdapter.notifyDataSetChanged();
                switch (checkid) {
                        case 1:
                                action_ID = mCustomActionData.get(i).getActionID();
                                break;
                        case 2:
                                action_ID = mCustomActionData.get(i).getActionID();
                                break;
                        case 3:
                                action_ID = mCustomActionData.get(i).getActionID();
                                break;
                        default:
                                break;
                }
        }

        /*获取头部动作*/
        private void getHeadData() {
                for (int i = 0; i < mHeadId.length; i++) {
                        mCustomActionData.add(new CustomActionData(mHeadId[i], R.drawable.custom_action_pic, mHead[i]));
                }
        }

        /*回去手部动作*/
        private void getHandData() {
                for (int i = 0; i < mArmId.length; i++) {
                        mCustomActionData.add(new CustomActionData(mArmId[i], R.drawable.custom_action_pic, mArm[i]));
                }
        }

        /*获取腿部动作*/
        private void getlegData() {
                for (int i = 0; i < mBootId.length; i++) {
                        mCustomActionData.add(new CustomActionData(mBootId[i], R.drawable.custom_action_pic, mBoot[i]));
                }
        }

        public void getData(View view) {
                resetImage();
                switch (view.getId()) {
                        case R.id.head_btn:
                                checkid = 1;
                                headIamgeView.setVisibility(View.VISIBLE);
                                break;
                        case R.id.hand_btn:
                                checkid = 2;
                                handIamgeView.setVisibility(View.VISIBLE);
                                break;
                        case R.id.leg_btn:
                                checkid = 3;
                                legIamgeView.setVisibility(View.VISIBLE);
                                break;
                        default:
                                break;
                }
                mHandler.sendEmptyMessage(checkid);
        }

        private void resetImage() {
                headIamgeView.setVisibility(View.GONE);
                handIamgeView.setVisibility(View.GONE);
                legIamgeView.setVisibility(View.GONE);
        }

        /*完成*/
        public void nextStep(View view) {
                if (!TextUtils.isEmpty(action_ID)) {
                        final Intent rIntent = new Intent();
                        rIntent.putExtra("action_ID", action_ID);
                        setResult(Activity.RESULT_OK, rIntent);
                        finish();
                }
                else {
                        ToastTools.newInstance(getApplicationContext()).showText(CustomActionActivity.this.getString(R.string.please_select_action));
                }
        }

        /*返回*/
        public void back(View view) {
                setResult(RESULT_CANCELED);
                finish();
        }

        /*跳过*/
        public void newDialogue(View view) {
                setResult(RESULT_CANCELED);
                finish();
        }
}
